package com.sczmgk.hotline.db.base;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.sczmgk.hotline.MyApp;
import com.sczmgk.hotline.utils.MyLog;

import static android.content.ContentValues.TAG;

public class DBHelper extends SQLiteOpenHelper {

    ////////////////////////////数据表start///////////////////
    /**
     * 用户信息表
     */
    public static final String V2_USER_INFO_TABLE = "user_info";


    ///////////////////////////数据表end/////////////////////

    /**
     * 数据版本号
     */
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "message.db";
    public static final String TABLE_ID = "_id";

    /**
     * 同步锁对象
     */
    public static final Object lockedObject = new Object();
    /**
     * 数据库核心对象
     */
    private static DBHelper dbHelper = null;

    private DBHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        MyLog.d(TAG, "当前使用数据库=" + getLoginUserName() + "_" + DATABASE_NAME);
    }

    /**
     * 智美服务器维一id
     */
    public static final String CHATID = "chatid";
    public static final String URL = "URL";

    /**
     * 聊天记录表
     */
    public static final String TABLE_CHAT_MSG = "table_chat_msg";
    public static final String FILENAME = "filename";
    public static final String MSGFROM = "msgfrom";
    public static final String GROUPNAME = "groupname";
    public static final String ISACKED = "isacked";
    public static final String ISDELIVERED = "isdelivered";
    public static final String ISLISTENED = "islistened";
    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE = "longitude";
    public static final String MSGDIR = "msgdir";
    public static final String MSGID = "msgid";
    public static final String MSGTIME = "msgtime";
    public static final String MSGTYPE = "msgtype";
    public static final String STATUS = "status";
    public static final String TEXTMSG = "textmsg";
    public static final String MSGTO = "msgto";
    public static final String TYPENAME = "typename";
    public static final String ISONLINE = "isonline";
    public static final String NEED_UPLOAD = "need_upload";

    /**
     * 未读消息数表
     */
    public static final String TABLE_NEW_MSG_COUNT = "table_new_msg_count";

    ///////////////////////////////////////用户表///////////////////////////////////////////////
    /**
     * 用户表
     */
    public static final String TABLE_USER = "table_user";

    // 用户名字
    public static final String USER_NAME = "user_name";

    // 用户id
    public static final String USERID = "uid";

    // 用户电话
    public static final String USER_PHONE = "user_phone";

    //环信帐号
    public static final String USER_HXACCOUNT = "user_hxaccount";

    //环信密码
    public static final String USER_HXPWD = "user_hxpwd";

    //头像路径
    public static final String USER_HEADURL = "user_headurl";

    /////////////////////////////////////////指示器标题表//////////////////////////////////////////////////

    /**
     * 标题指示器表(indicator头)
     */
    public static final String INDICATOR_TITLE_TABLE = "indicator_title_table";

    /**
     * 标题指示器的标题
     */
    public static final String INDICATOR_TITLE = "indicator_title";

    /**
     * 标题指示器的类型
     */
    public static final String INDICATOR_TYPE = "indicator_type";

    /**
     * 标题指示器id
     */
    public static final String INDICATOR_ID = "indicator_id";

    @Override
    public void onCreate(SQLiteDatabase db) {
        createDB(db);
    }

    private void createDB(SQLiteDatabase db) {
        //用户表
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_USER + " (" + TABLE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + USERID + " TEXT,"
                + USER_NAME + " TEXT,"
                + USER_HXACCOUNT + " TEXT,"
                + USER_HXPWD + " TEXT,"
                + USER_HEADURL + " TEXT,"
                + USER_PHONE + " TEXT)";
        db.execSQL(sql);

        //聊天消息表
        sql = "CREATE TABLE IF NOT EXISTS " + TABLE_CHAT_MSG + " (" + TABLE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + FILENAME + " TEXT,"
                + MSGFROM + " TEXT,"
                + GROUPNAME + " TEXT,"
                + ISACKED + " TEXT,"
                + ISDELIVERED + " TEXT,"
                + ISLISTENED + " TEXT,"
                + LATITUDE + " TEXT,"
                + LONGITUDE + " TEXT,"
                + MSGDIR + " TEXT,"
                + MSGID + " TEXT,"
                + MSGTIME + " TEXT,"
                + MSGTYPE + " integer,"
                + STATUS + " TEXT,"
                + TEXTMSG + " TEXT,"
                + MSGTO + " TEXT,"
                + TYPENAME + " TEXT,"
                + URL + " TEXT,"
                + USERID + " TEXT,"
                + NEED_UPLOAD + " integer default 0,"
                + ISONLINE + " TEXT,"
                + CHATID + " TEXT UNIQUE)";
        db.execSQL(sql);

        //新消息数量表
        sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NEW_MSG_COUNT + " (" + TABLE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + USERID + " TEXT,"
                + MSGDIR + " TEXT," + MSGID + " TEXT," + CHATID + " TEXT UNIQUE)";
        db.execSQL(sql);

        //指示器标题表
        sql = "CREATE TABLE IF NOT EXISTS " + INDICATOR_TITLE_TABLE + " (" + TABLE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + INDICATOR_TITLE + " TEXT," + INDICATOR_ID + " TEXT,"
                + INDICATOR_TYPE + " TEXT)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        for (int i = oldVersion; i < newVersion; i++) {
            switch (i) {
                case 1:
                    version1ToVersion2(db);
                    break;
                case 2:
                    version2ToVersion3(db);
                    break;
                case 3:
                    version3ToVersion4(db);
                    break;
            }
        }
    }

    /**
     * 增加区域表,功能表(存图片路径)
     *
     * @param db
     */
    private void version1ToVersion2(SQLiteDatabase db) {
        //不处理
    }

    /**
     * 给搜索列表加入用户id
     *
     * @param db
     */
    private void version2ToVersion3(SQLiteDatabase db) {
        //不处理
    }

    /**
     * 删除2.0版本数据库
     *
     * @param db
     */
    private void version3ToVersion4(SQLiteDatabase db) {
        //TODO
        //1.删除之前的数据库
//        FileUtil.clearAllData(context);
        String sql = "drop table " + V2_USER_INFO_TABLE;
        db.execSQL(sql);
        createDB(db);
    }

    /**
     * 取得登录的用户名
     *
     * @return
     */
    private static String getLoginUserName() {
        return MyApp.loginUserName;
    }

    /**
     * 关闭资源
     *
     * @param db
     * @param cursor
     */
    public static void closeAll(SQLiteDatabase db, Cursor cursor) {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        if (db != null) {
            // db.close();
        }
    }

    /**
     * 销毁dbHelper
     */
    public static void destoryDBHelper() {
        synchronized (lockedObject) {
            if (dbHelper != null) {
                dbHelper.close();
                dbHelper = null;
            }
        }
    }

    /**
     * 获取数据库核心类单例对象
     *
     * @param context
     * @return
     */
    public static DBHelper getInstance(Context context) {
        synchronized (DBHelper.lockedObject) {
            if (dbHelper == null) {
                dbHelper = new DBHelper(context);
            }
            return dbHelper;
        }
    }
}
